package com.tianbo.controller;

import com.tianbo.common.ModelAndPage;
import com.tianbo.model.TOlapColumn;
import com.tianbo.model.TOlapXmldoc;
import com.tianbo.model.TOlapXmlnode;
import com.tianbo.service.OlapColumnService;
import com.tianbo.service.OlapXmldocService;
import com.tianbo.service.OlapXmlnodeService;
import com.tianbo.util.dao.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Mr.Xu on 2017/9/14.
 */
@Controller
@RequestMapping("/olapxmlnode")
public class OlapXmlnodeController {

    @Autowired
    private OlapXmlnodeService olapXmlnodeService;
    @Autowired
    private OlapXmldocService olapXmldocService;
    @Autowired
    private OlapColumnService olapColumnService;
    @RequestMapping("showlist")
    public ModelAndView findXmlnode(@ModelAttribute Page page)throws Exception{
        int pagelenth=10;
        int start=0;
        if(page.getBegin()>=0){start=page.getBegin();}
        if(page.getLength()>0){pagelenth=page.getLength();}
        ModelAndView modelAndView=new ModelAndView();
        ModelAndPage modelAndPage=new ModelAndPage();
        modelAndPage=olapXmlnodeService.findXmlnode(start,pagelenth);
        List<TOlapXmldoc> olapXmldocList=olapXmldocService.findXmldoc();
        List<TOlapColumn> olapColumnList=olapColumnService.findOlapColumn();
        modelAndView.addObject("olapXmldocList",olapXmldocList);
        modelAndView.addObject("olapColumnList",olapColumnList);
        modelAndView.addObject("modelAndPage",modelAndPage);
        modelAndView.setViewName("olapxmlnode/list");
        return modelAndView;
    }

    @RequestMapping("addXmlnode")
    public String addXmlnode(HttpServletRequest request,TOlapXmlnode olapXmlnode,RedirectAttributes attr){
        String docid=request.getParameter("docid");
        String columnid=request.getParameter("columnid");
        olapXmlnode.setDocid(new BigDecimal(docid));
        olapXmlnode.setColumnid(new BigDecimal(columnid));
        olapXmlnodeService.addXmlnode(olapXmlnode);
        attr.addAttribute("docid",new BigDecimal(docid));
        return "redirect:/olapxmlnode/selectlist";
    }
    @RequestMapping("findByid")
    public ModelAndView findByid(HttpServletRequest request,BigDecimal nodeid)throws Exception{
        ModelAndView modelAndView=new ModelAndView();
        String id=request.getParameter("nodeid");
        TOlapXmlnode olapXmlnode=olapXmlnodeService.findByid(new BigDecimal(id));
        List<TOlapXmldoc> olapXmldocList=olapXmldocService.findXmldoc();
        List<TOlapColumn> olapColumnList=olapColumnService.findOlapColumn();
        modelAndView.addObject("olapXmldocList",olapXmldocList);
        modelAndView.addObject("olapColumnList",olapColumnList);
        modelAndView.addObject("olapXmlnode",olapXmlnode);
        modelAndView.setViewName("olapxmlnode/edit");
        return modelAndView;
    }
    @RequestMapping("selectlist")
    public ModelAndView selectXmlnode(HttpServletRequest request,BigDecimal docid)throws Exception{
        ModelAndView modelAndView=new ModelAndView();
        String id=request.getParameter("docid");
        BigDecimal bd=new BigDecimal(id);
        List<TOlapXmlnode> tOlapXmlnodeList=olapXmlnodeService.selectXmlnode(bd);
        TOlapXmldoc tOlapXmldoc=olapXmldocService.findByid(bd);
        List<TOlapColumn> olapColumnList=olapColumnService.findOlapColumn();
        modelAndView.addObject("tOlapXmlnodeList",tOlapXmlnodeList);
        modelAndView.addObject("tOlapXmldoc",tOlapXmldoc);
        modelAndView.addObject("olapColumnList",olapColumnList);
        modelAndView.setViewName("olapxmlnode/list");
        return modelAndView;
    }
    @RequestMapping("updateXmlnode")
    public String updateXmlnode(HttpServletRequest request,TOlapXmlnode olapXmlnode,RedirectAttributes attr){
        String nodeid=request.getParameter("nodeid");
        String docid=request.getParameter("docid");
        String columnid=request.getParameter("columnid");
        olapXmlnode.setColumnid(new BigDecimal(columnid));
        olapXmlnode.setDocid(new BigDecimal(docid));
        olapXmlnode.setNodeid(new BigDecimal(nodeid));
        olapXmlnodeService.updateXmlnode(olapXmlnode);
        attr.addAttribute("docid",new BigDecimal(docid));
        return "redirect:/olapxmlnode/selectlist";
    }
    @RequestMapping("deleteByPrimaryKey")
    public String deleteByPrimaryKey(HttpServletRequest request,BigDecimal nodeid,RedirectAttributes attr){
        String id=request.getParameter("nodeid");
        String docid=request.getParameter("docid");
        olapXmlnodeService.deleteByPrimaryKey(new BigDecimal(id));
        attr.addAttribute("docid",new BigDecimal(docid));
        return "redirect:/olapxmlnode/selectlist";
    }
    @RequestMapping("list")
    public String list(){return "olapxmlnode/list";}
}
